Ext.onReady(function() {
                
                // display tooltips for actions
                Ext.QuickTips.init();
               
                // estilo para la capa de partido
                
                var style_barrios = new OpenLayers.StyleMap({ 
		            "default": new OpenLayers.Style( 
		            { 
		                pointRadius: 3, 
		                strokeColor: "orange", 
		                fillColor: "transparent", 
		                fillOpacity: 0.9, 
		                strokeWidth: 4
		            }) 
		        });

                var style_partido = new OpenLayers.StyleMap({ 
                    "default": new OpenLayers.Style( 
                    { 
                        pointRadius: 3, 
                        strokeColor: "red", 
                        fillColor: "transparent", 
                        fillOpacity: 0.9, 
                        strokeWidth: 4
                    }) 
                });

                saveStrategy=new OpenLayers.Strategy.Save();

                saveStrategy.events.register('start',this,function(){
                    //console.log(" apunto de commitear");
                });

                saveStrategy.events.register('success', this, function(e){
                    var code=e.response.code;
                    if(code==1){
                        select.unselectAll();
                        alert("Se guardo exitosamente");
                    }else{
                        alert("No se pudo guardar el barrio...codigo: "+code);
                    }
                });
                
                saveStrategy.events.register('fail', this, function(e){
                    //var code=e.response.code;
                    alert(e.response.priv.responseText);
                    resetearDibujos();
                });

                // create a vector layer that will contain features
                barrios = new OpenLayers.Layer.Vector('Barrios', {
                    projection:"EPSG:900913", 
                    styleMap: style_barrios,
                    strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy],    
                    protocol: new OpenLayers.Protocol.WFS({
                    url: "http://www.sstuv.gba.gov.ar/geoserver/wfs",
                    version: "1.0.0",
                    //featureType: nombre_capa_editable_barrios,
                    featureType: 'barrios',
                    featureNS: 'sstuyv',
                    srsName: 'EPSG:900913',
                    maxFeatures: 100
                    })
            
                });

                var barrios_wms=new OpenLayers.Layer.WMS("Barrios",
                		"http://www.sstuv.gba.gov.ar//geoserver/wms",
                		{layers:"barrios", transparent:true}
                		);
                
                
                partido=new OpenLayers.Layer.Vector("partidos_vector",{styleMap:style_partido});
		        
                // create a control for selecting features
                select = new OpenLayers.Control.SelectFeature(barrios);
                
                barrios.events.on({
                    featureselected: function(event) {
                        // create an open a popup when a feature is selected
                        var attributes=event.feature.attributes;

                        var esVacio=true;

                        for(var key in attributes) {
                            if(attributes.hasOwnProperty(key))
                                esVacio = false;
                        }   
                        
                        if(esVacio){
                            console.log('se crearon los atributos');
                            event.feature.attributes={	nombre: "",
                            							id_partido: cod_partido,
                            							codcatas: cod_partido,
                            							"Nomenclatura_origen": nomencla_origen};
                        }

                        popup = new GeoExt.Popup({
                            title: "Atributos",
                            id:"popup_feature",
                            feature: event.feature,
                            collapsible: true,
                            layout: "fit",
                            width: 250,
                            border: false,
                            listeners: {
                                close: function() {
                                    select.unselectAll();
                                }
                            },
                            items: [
                                new Ext.grid.PropertyGrid({
                                    autoHeight: true,                                 
                                    source: event.feature.attributes,
                                    bbar:[guardar,cancelar]
                                })
                            ]
                        });
                        popup.show();
                    },
                    featureunselected: function(event) {
                        // close (and destroy) the popup when the feature is unselected
                        popup.destroy();
                    }
                });	

               
                
                 map = new OpenLayers.Map({
                    projection: "EPSG:900913",
                    //maxExtent: new OpenLayers.Bounds(-7201468.525267,-5306727.239458,-5949124.254017,-3839136.296587),
                    layers: [

                        new OpenLayers.Layer.Google(
                            "Google Hybrid",
                             {type: google.maps.MapTypeId.HYBRID, numZoomLevels: 20}
                        ),
                        new OpenLayers.Layer.Google(
                            "Google Physical",
                            {type: google.maps.MapTypeId.TERRAIN}
                        ),
                        new OpenLayers.Layer.Google(
                            "Google Streets", // the default
                            {numZoomLevels: 20}
                        ),
                        new OpenLayers.Layer.Google(
                            "Google Satellite",
                            {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22}
                        ),
                        new OpenLayers.Layer.XYZ(
                                "OpenStreetMap", 
                            [
                             "http://otile1.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.png",
                             "http://otile2.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.png",
                             "http://otile3.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.png",
                             "http://otile4.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.png"
                            ]
                        ),barrios,partido,barrios_wms
                    ]
                  });
                 map.addControl(new OpenLayers.Control.LayerSwitcher());
                 
                panel = new GeoExt.MapPanel({
                    title: "MapPanel",
                    renderTo: "map-id",
                    height: 500,
                    width: "100%",
                    map:map,
                    extent:[-8267315.703574,-5098940.045483,-5070413.43302,-3941989.18552]
                  
                });
                
                map.addControl(select);
                select.activate();
                // create an action tied to a navigation control
                navegar = new GeoExt.Action({
                    text: "Navegar",
                    control: new OpenLayers.Control.Navigation(),
                    map: panel.map,
                    // button options
                    toggleGroup: "draw",  // only one tool can be active in a group
                    allowDepress: false,
                    pressed: true,
                    tooltip: "navegar el mapa"
                });
                
                // create an action tied to control for drawing polygons
                dibujar = new GeoExt.Action({
                    text: "Dibujar Barrio",
                    control: new OpenLayers.Control.DrawFeature(
                        barrios, OpenLayers.Handler.Polygon,
                        {multi: true}
                    ),
                    map: panel.map,
                    // button options
                    toggleGroup: "draw",  // only one tool can be active in a group
                    allowDepress: false,
                    tooltip: "Dibujar el barrio en el mapa"
                });
                
                dibujar.control.events.register('featureadded', this, finDibujo);

                // create an action tied to control for drawing polygons
                guardar = new GeoExt.Action({
                    text: "Guardar",
                    control: new OpenLayers.Control(),
                    map: panel.map,
                    handler: function(){
                        /*if(ol.modifyFeature.feature) {
                            //edit.selectControl.unselectAll();
                            ol.modifyFeature.unselectFeature();
                        }*/
                        saveStrategy.save();
                    },
                    // button options
                    toggleGroup: "draw",  // only one tool can be active in a group
                    allowDepress: false,
                    tooltip: "Guardar el dibujo del barrio"
                });

                cancelar = new GeoExt.Action({
                    text: "Cancelar",
                    control: new OpenLayers.Control(),
                    map: panel.map,
                    handler: function(event){
                     resetearDibujos();                         
                    },
                    // button options
                    toggleGroup: "draw",  // only one tool can be active in a group
                    allowDepress: false,
                    tooltip: "Cancelar el dibujo del barrio"
                });

                resetear = new GeoExt.Action({
                    text: "Resetear",
                    control: new OpenLayers.Control(),
                    map: panel.map,
                    handler: function(event){
                     resetearDibujos();  
                     toolbar.items.items[1].toggle();                       
                    },
                    // button options
                    toggleGroup: "draw",  // only one tool can be active in a group
                    allowDepress: false,
                    tooltip: "Borrar el dibujo actual del barrio"
                });

                // add a toolbar with the above actions as buttons
                toolbar = new Ext.Toolbar({
                    renderTo: "tbar-id",
                    items: [navegar,dibujar,resetear]
                });
                
                map.setPartido = function(p){
             	var url = "http://www.sstuv.gba.gov.ar/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=partidos_arba_depto_indec&maxFeatures=5&outputFormat=json&srsName=epsg:900913&cql_filter=nomencla=%27"+p+"%27";
				
				        OpenLayers.loadURL(url,{},this,onComplete,onFailure);
			
				        function onComplete(r){
		                    var formato=new OpenLayers.Format.GeoJSON();
					        var response=formato.read(r.responseText);
					        var features=response;
						    if(partido.features.length > 0){
		                        partido.destroyFeatures();	
		                    }
				        	partido.addFeatures(features);
				        	var extent=partido.getDataExtent();
				        	map.zoomToExtent(extent);
					
				        }
		
				        function onFailure(r){
				        	alert("fallo al obtener el extent del partido");
				        }

		          };
	

            });


function finDibujo(e){
 select.select(e.feature);
}

function resetearDibujos(){

    for(var f in barrios.features){
        var feature=barrios.features[f];
        if(feature.state=="Insert"){
            barrios.removeFeatures(feature);
        }
    }
    popup=Ext.getCmp("popup_feature");
    if (popup) {
        popup.destroy();
    };
    
    select.unselectAll();
}
